package com.example.opengldemo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.Bundle;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MainActivity extends AppCompatActivity {

    GLSurfaceView glSurfaceView;
    boolean supportEs2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        glSurfaceView = findViewById(R.id.gl_surface_view);

        ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
        supportEs2 = configurationInfo.reqGlEsVersion>=0x20000;
        if(!supportEs2){
            Log.e("androidLog","Not support es2");
            return;
        }

        glSurfaceView.setEGLContextClientVersion(2);

        glSurfaceView.setRenderer(new MyRenderer(this));
    }

    public static String readText(Context context,int resourceId){

        StringBuilder sb = new StringBuilder();
        InputStream is = context.getResources().openRawResource(resourceId);
        byte b[] = new byte[1024];
        int len=0;
        try {
            while ((len=is.read(b))>0){
                sb.append(new String(b,0,len));
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return sb.toString();
    }

    class MyRenderer implements GLSurfaceView.Renderer{

        private Context context;


        private float[] projectionMatrix = new float[16];
        private float[] modelMatrix = new float[16];

        private Table table;
        private Mallet mallet;

        private TextureShaderProgram textureShaderProgram;
        private ColorShaderProgram colorShaderProgram;

        private int texture;

        public MyRenderer(Context context){
            this.context = context;
        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            GLES20.glClearColor(0,0,0,0);
            table = new Table();
            mallet = new Mallet();

            textureShaderProgram = new TextureShaderProgram(context);
            colorShaderProgram = new ColorShaderProgram(context);

            texture = TextureHelper.loadTexture(context,R.drawable.bb);

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            GLES20.glViewport(0,0,width,height);
            Matrix.setIdentityM(projectionMatrix,0);

        }

        @Override
        public void onDrawFrame(GL10 gl) {

            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

            textureShaderProgram.userProgram();
            textureShaderProgram.setUniforms(projectionMatrix,texture);
            table.bindData(textureShaderProgram);
            table.draw();

            colorShaderProgram.userProgram();
//            Matrix.rotateM(projectionMatrix,0,-60f,1f,0f,0f);
            colorShaderProgram.setUniforms(projectionMatrix);
            mallet.bindData(colorShaderProgram);
            mallet.draw();

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(supportEs2){
            glSurfaceView.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(supportEs2){
            glSurfaceView.onResume();
        }
    }
}
